home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Imaging / Bounce < prev    next >
Encoding:
Text File  |  1999-03-04  |  5.0 KB  |  216 lines  |  [TEXT/ToyS]

  1. property kasMinSpeed : 2
  2. property kasMaxSpeed : 20
  3. property kasErase : true
  4. property kasName : "Bounce V1.1"
  5.  
  6. global gasWinLoc -- Location of window
  7.  
  8. on run
  9.     pfLoad()
  10.     idle
  11. end run
  12.  
  13.  
  14. on idle
  15.     set dims to random number from 250 to 500
  16.     set ballDim to random number from 25 to 125
  17.     
  18.     set drawWin to ¬
  19.         display drawing titled ("Bounce 1.1 - shift=quit, w/opt to toggle trace") ¬
  20.             located at gasWinLoc ¬
  21.             with dimensions {dims, dims}
  22.     
  23.     set gasWinLoc to screen location of drawWin -- Save for comparison
  24.     
  25.     draw a box into drawWin ¬
  26.         inside of {0, 0, dims, dims} ¬
  27.         filling it by erasing it ¬
  28.         using state {bg col64:{0, 0, 0}}
  29.     
  30.     set rgn to CreateRgn(ballDim)
  31.     set ball to CreateBall(ballDim, rgn)
  32.     
  33.     set bbox to {0, 0, ballDim, ballDim}
  34.     set sY to random number from kasMinSpeed to kasMaxSpeed
  35.     set sX to random number from kasMinSpeed to kasMaxSpeed
  36.     
  37.     set userState to input state
  38.     
  39.     set i to 0
  40.     set erase to kasErase
  41.     
  42.     repeat until (shift key down of userState) or (i > 9999)
  43.         set item 1 of bbox to (item 1 of bbox) + sX
  44.         set item 3 of bbox to (item 3 of bbox) + sX
  45.         set item 2 of bbox to (item 2 of bbox) + sY
  46.         set item 4 of bbox to (item 4 of bbox) + sY
  47.         
  48.         -- Hit bottom?
  49.         if (item 4 of bbox ≥ dims) then
  50.             set sY to -2 * (random number from kasMinSpeed to kasMaxSpeed)
  51.             set item 2 of bbox to dims - ballDim
  52.             set item 4 of bbox to dims
  53.         end if
  54.         
  55.         -- Hit right?
  56.         if (item 3 of bbox ≥ dims) then
  57.             set sX to -(random number from kasMinSpeed to kasMaxSpeed)
  58.             set item 1 of bbox to dims - ballDim
  59.             set item 3 of bbox to dims
  60.         end if
  61.         
  62.         -- Hit top?
  63.         if (item 2 of bbox < 0) then
  64.             set sY to round ((random number from kasMinSpeed to kasMaxSpeed) / 2)
  65.             set item 2 of bbox to 0
  66.             set item 4 of bbox to ballDim
  67.         end if
  68.         
  69.         -- Hit left?
  70.         if (item 1 of bbox < 0) then
  71.             set sX to (random number from kasMinSpeed to kasMaxSpeed)
  72.             set item 1 of bbox to 0
  73.             set item 3 of bbox to ballDim
  74.         end if
  75.         
  76.         draw a picture into drawWin ¬
  77.             using data ball ¬
  78.             inside of bbox ¬
  79.             without recording
  80.         
  81.         if (erase) then
  82.             set ballRgn to calculate region from the mapping ¬
  83.                 of region rgn inside of bbox
  84.             set diffRgn to calculate region from the difference ¬
  85.                 of region rgn with region ballRgn
  86.             
  87.             draw a region into drawWin ¬
  88.                 using data diffRgn ¬
  89.                 filling it by erasing it ¬
  90.                 without recording
  91.             
  92.             set rgn to ballRgn
  93.         end if
  94.         
  95.         set userState to input state
  96.         set i to i + 1
  97.         set sY to sY + 1 -- Gravity
  98.     end repeat
  99.     
  100.     set winLoc to screen location of ¬
  101.         (display drawing drawWin with disposal)
  102.     
  103.     if (winLoc is not gasWinLoc) then
  104.         set gasWinLoc to winLoc
  105.         pfSave()
  106.     end if
  107.     
  108.     if (option key down of userState) then
  109.         set kasErase to not kasErase
  110.         return 1
  111.     end if
  112.     
  113.     return 60
  114. end idle
  115.  
  116.  
  117. on CreateBall(dim, rgn)
  118.     set ballWin to display drawing titled ¬
  119.         "Creating Ball" with dimensions {dim, dim}
  120.     
  121.     set fgc to {} & ¬
  122.         {random number from 444 to 44444} & ¬
  123.         {random number from 444 to 44444} & ¬
  124.         {random number from 444 to 44444}
  125.     set box to {0, 0, dim, dim}
  126.     
  127.     set cnt to round (dim / 3)
  128.     
  129.     set colStepR to round ((65000 - (item 1 of fgc)) / cnt)
  130.     set colStepG to round ((65000 - (item 2 of fgc)) / cnt)
  131.     set colStepB to round ((65000 - (item 3 of fgc)) / cnt)
  132.     
  133.     repeat with i from 1 to cnt
  134.         draw an oval into ballWin ¬
  135.             inside of box ¬
  136.             using state {fg col64:fgc, pen size:{2, 2}}
  137.         
  138.         set item 3 of box to (item 3 of box) - 1
  139.         set item 4 of box to (item 4 of box) - 1
  140.         
  141.         draw an oval into ballWin ¬
  142.             inside of box ¬
  143.             using state {fg col64:fgc, pen size:{1, 1}}
  144.         
  145.         set item 1 of box to (item 1 of box) + 1
  146.         set item 2 of box to (item 2 of box) + 1
  147.         set item 3 of box to (item 3 of box) - 1
  148.         set item 4 of box to (item 4 of box) - 1
  149.         
  150.         set item 1 of fgc to (item 1 of fgc) + colStepR
  151.         set item 2 of fgc to (item 2 of fgc) + colStepG
  152.         set item 3 of fgc to (item 3 of fgc) + colStepB
  153.     end repeat
  154.     
  155.     set ball to ¬
  156.         capture picture from ballWin ¬
  157.             inside region rgn ¬
  158.             using depth DepthOfMonitorContaining(screen location of ballWin) ¬
  159.             with pixel conversion and dithering
  160.     
  161.     display drawing ballWin with disposal
  162.     
  163.     return ball
  164. end CreateBall
  165.  
  166.  
  167. on DepthOfMonitorContaining(pt)
  168.     set displays to the display information
  169.     repeat with dsp in displays
  170.         if PtInRect(pt, monitor box of dsp) then ¬
  171.             return monitor depth of dsp
  172.     end repeat
  173.     return monitor depth of item 1 of displays
  174. end DepthOfMonitorContaining
  175.  
  176.  
  177. on PtInRect(pt, rc)
  178.     return (item 1 of pt ≥ item 1 of rc) ¬
  179.         and (item 2 of pt ≥ item 2 of rc) ¬
  180.         and (item 1 of pt ≤ item 3 of rc) ¬
  181.         and (item 2 of pt ≤ item 4 of rc)
  182. end PtInRect
  183.  
  184.  
  185. on CreateRgn(dim)
  186.     set ballWin to display drawing titled ¬
  187.         "Creating Region" with dimensions {dim, dim}
  188.     
  189.     set box to {0, 0, dim, dim}
  190.     
  191.     draw an oval into ballWin ¬
  192.         inside of box
  193.     
  194.     set rgn to capture region from ballWin
  195.     
  196.     display drawing ballWin with disposal
  197.     
  198.     return rgn
  199. end CreateRgn
  200.  
  201.  
  202. on pfLoad()
  203.     try
  204.         set myPref to load preference named kasName
  205.     on error
  206.         set myPref to {{-1, -1}}
  207.     end try
  208.     
  209.     set gasWinLoc to item 1 of myPref
  210. end pfLoad
  211.  
  212.  
  213. on pfSave()
  214.     save preference {gasWinLoc} named kasName
  215. end pfSave
  216.